﻿namespace BlackHole
{
    using System;
    using System.Web.UI.WebControls;
    using BlackHole.DAO;

    /// <summary>
    /// 工作事务页面
    /// </summary>
    public partial class Workflow : System.Web.UI.Page
    {
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RenderUI();
            }
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        protected void RenderUI()
        {
            /////fill district list
            foreach (District d in Utility.GetAllDistricts())
            {
                comboDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }
            /////fill owner list of first distict id
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(comboDistricts.Items[0].Value)))
            {
                comboOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }

            /////fill device list
            comboDevices.Items.Add(new ListItem("(全部)", "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                comboDevices.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }

            /////fill product status
            foreach (Utility.ProductStatus s in Enum.GetValues(typeof(Utility.ProductStatus)))
            {
                radioStatus.Items.Add(new ListItem(s.ToString(), ((int)s).ToString()));
            }

            radioStatus.SelectedIndex = 1;

            /////set default datasource
            dataProducts.AllowPaging = false;
            dataProducts.DataSourceID = sourceGetProductBySerial.ID;
            dataProducts.DataBind();

            /////fill add new panel
            comboAddDevice.Items.Add(new ListItem(string.Empty, "0"));
            foreach (Device d in Utility.GetAllDevices())
            {
                comboAddDevice.Items.Add(new ListItem(d.DeviceName, d.Id.ToString()));
            }

            foreach (District d in Utility.GetAllDistricts())
            {
                comboAddDistricts.Items.Add(new ListItem(d.DistrictName, d.Id.ToString()));
            }

            txtSearchSerial.Focus();
        }

        /// <summary>
        /// 事件 文本框更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TxtSearchSerial_TextChanged(object sender, EventArgs e)
        {
            txtSearchSerial.Text = txtSearchSerial.Text.Replace("S/N:", string.Empty);
            if (txtSearchSerial.Text.Length > 0)
            {
                /////fix select all when search by serial
                radioStatus.SelectedIndex = 0;
                dataProducts.DataSourceID = sourceGetProductBySerial.ID;
                dataProducts.DataBind();
                dataProducts.SelectedIndex = 0;
                this.DataProducts_SelectedIndexChanged(sender, e);
            }
        }

        /// <summary>
        /// 事件 更改区县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboOwners.Items.Clear();
            comboOwners.Items.Add(new ListItem("(全部)", "0"));
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(comboDistricts.SelectedValue)))
            {
                comboOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }

            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 更改设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboDevices_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 更改网点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboOwners_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 更改产品状态筛选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadioStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dataProducts.DataSourceID == sourceGetProductByOwner.ID)
            {
                dataProducts.DataSourceID = sourceGetProductByOwner.ID;
            }
            else
            {
                dataProducts.DataSourceID = sourceGetProductBySerial.ID;
            }

            dataProducts.DataBind();
            this.HideAllPanel();
        }

        /// <summary>
        /// 事件 产品选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            if (dataProducts.Rows.Count > 0)
            {
                txtSelectedDevice.Text = dataProducts.SelectedDataKey["ProductDeviceName"].ToString();
                txtSelectedPsamNO.Text = dataProducts.SelectedDataKey["PsamNO"].ToString();
                txtSelectedSerialNO.Text = dataProducts.SelectedDataKey["SerialNO"].ToString();
                lblSelectedOwner.Text =
                    dataProducts.SelectedDataKey["ProductDistrictName"].ToString() +
                    " " +
                    dataProducts.SelectedDataKey["ProductOwnerName"].ToString();
                Utility.ProductStatus status =
                    (Utility.ProductStatus)int.Parse(dataProducts.SelectedDataKey["StatusId"].ToString());
                switch (status)
                {
                    case Utility.ProductStatus.全部:
                        break;
                    case Utility.ProductStatus.在库:
                        this.HideAllPanel();
                        this.btnOut.Visible = true;

                        break;
                    case Utility.ProductStatus.待修:
                        this.HideAllPanel();
                        this.panelProductRepair.Visible = true;
                        this.btnRepair.Enabled = false;
                        this.btnRepair.Visible = true;
                        break;
                    case Utility.ProductStatus.不在库:
                        this.HideAllPanel();
                        this.panelProductUnavailable.Visible = true;
                        this.btnIn.Visible = true;
                        this.chkCheckIn.Visible = true;
                        this.btnRequestRepair.Visible = false;
                        break;
                    default:
                        break;
                }
                /////fill real and request faults list
                this.comboRequestFault.Items.Clear();
                this.comboRealFault.Items.Clear();
                this.chkCheckIn.Checked = false;

                this.comboRequestFault.Items.Add(new ListItem(string.Empty, "0"));
                this.comboRealFault.Items.Add(new ListItem(string.Empty, "0"));
                foreach (Fault f in Utility.GetFaultsByDeviceId(int.Parse(dataProducts.SelectedDataKey["ProductDeviceId"].ToString())))
                {
                    this.comboRequestFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
                    this.comboRealFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
                }
            }
            else
            {
                this.HideAllPanel();
                this.panelProductInformation.Visible = false;
                this.btnAddNew.Visible = true;
                this.panelProductAddNew.Visible = true;
                this.panelProductUnavailable.Visible = true;
                this.panelProductUnavailable.Enabled = false;
                this.txtAddSerialNO.Text = txtSearchSerial.Text;

                this.comboAddDevice.SelectedIndex = -1;
                this.comboAddDistricts.SelectedIndex = -1;
                this.txtRepairGear.Text = string.Empty;
                this.txtRepairMethod.Text = string.Empty;
            }
        }

        /// <summary>
        /// 隐藏并初始化控件
        /// </summary>
        protected void HideAllPanel()
        {
            this.panelProductAddNew.Visible = false;
            this.panelProductRepair.Visible = false;
            this.panelProductUnavailable.Visible = false;
            this.btnAddNew.Visible = false;
            this.btnIn.Visible = false;
            this.btnIn.Enabled = false;
            this.chkCheckIn.Visible = false;
            this.btnOut.Visible = false;
            this.btnRepair.Visible = false;
            this.btnRequestRepair.Visible = false;
            this.btnAddAndRepair.Visible = false;
            this.btnAddAndRepair2.Visible = false;
            this.panelProductInformation.Visible = true;
        }

        /// <summary>
        /// 事件 更改报修故障
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboRequestFault_SelectedIndexChanged(object sender, EventArgs e)
        {
            /////报修故障被选中
            if (int.Parse(comboRequestFault.SelectedValue) > 0)
            {
                /////新增产品
                if (this.btnAddNew.Visible == true)
                {
                    this.HideAllButton();
                    this.btnAddNew.Visible = false;
                    this.btnAddAndRepair.Visible = true;
                }
                else
                {
                    /////入库维修产品
                    this.HideAllButton();
                    this.btnIn.Visible = false;
                    this.chkCheckIn.Visible = false;
                    this.btnRequestRepair.Visible = true;
                }

                this.panelProductRepair.Visible = true;
            }
            else
            {
                if (this.btnAddAndRepair.Visible == true)
                {
                    this.HideAllButton();
                    this.btnAddAndRepair.Visible = false;
                    this.btnAddNew.Visible = true;
                }
                else
                {
                    this.HideAllButton();
                    this.btnRequestRepair.Visible = false;
                    this.btnIn.Visible = true;
                    this.chkCheckIn.Visible = true;
                }

                this.panelProductRepair.Visible = false;
            }
        }

        /// <summary>
        /// 隐藏所有按钮
        /// </summary>
        protected void HideAllButton()
        {
            this.btnAddAndRepair.Visible = false;
            this.btnAddAndRepair2.Visible = false;
            this.btnAddNew.Visible = false;
            this.btnIn.Visible = false;
            this.btnOut.Visible = false;
            this.btnRepair.Visible = false;
            this.btnRequestRepair.Visible = false;
        }

        /// <summary>
        /// 事件 更改实际故障
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboRealFault_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(comboRealFault.SelectedValue) > 0)
            {
                /////新增产品
                if (this.btnAddAndRepair.Visible == true)
                {
                    this.btnAddAndRepair.Visible = false;
                    this.btnAddAndRepair2.Visible = true;
                }
                else
                {
                    /////入库的产品维修
                    this.btnRequestRepair.Visible = false;
                    this.btnRepair.Visible = true;
                    this.btnRepair.Enabled = true;
                }

                if (txtRepairGear.Text.Length == 0)
                {
                    this.txtRepairGear.Text = "无";
                }

                if (this.txtRepairMethod.Text.Length == 0)
                {
                    this.txtRepairMethod.Text = "无";
                }
            }
            else
            {
                if (this.btnAddAndRepair2.Visible == true)
                {
                    this.btnAddAndRepair2.Visible = false;
                    this.btnAddAndRepair.Visible = true;
                }
                else
                {
                    this.btnRepair.Visible = false;
                    this.btnRequestRepair.Visible = true;
                }
            }
        }

        /// <summary>
        /// 事件 选中直接入库复选框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChkCheckIn_CheckedChanged(object sender, EventArgs e)
        {
            this.btnIn.Enabled = this.chkCheckIn.Checked;
        }

        /// <summary>
        /// 事件 分页 保留
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.dataProducts.PageIndex = e.NewPageIndex;
            this.dataProducts.DataSourceID = this.sourceGetProductByOwner.ID;
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 更改区县 新增产品状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboAddDistricts_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboAddOwners.Items.Clear();
            foreach (Owner o in Utility.GetOwnersByDistrictId(int.Parse(this.comboAddDistricts.SelectedValue)))
            {
                this.comboAddOwners.Items.Add(new ListItem(o.OwnerName, o.Id.ToString()));
            }

            this.panelProductUnavailable.Enabled = true;
        }

        /// <summary>
        /// 事件数据表绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DataProducts_DataBinding(object sender, EventArgs e)
        {
            this.dataProducts.AllowPaging = true;
            this.dataProducts.SelectedIndex = -1;
        }

        /// <summary>
        /// 事件 产品入库按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnIn_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductCheckIn(int.Parse(this.dataProducts.SelectedDataKey["Id"].ToString()));
            this.lblError.Text = ret ? "入库成功" : "入库失败";
            this.HideAllPanel();
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 产品出库按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnOut_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductCheckOut(int.Parse(this.dataProducts.SelectedDataKey["Id"].ToString()));
            this.lblError.Text = ret ? "出库成功" : "出库失败";
            this.HideAllPanel();
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 新增产品按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddNew_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductAddNew(
                int.Parse(comboAddDevice.SelectedValue),
                int.Parse(comboAddOwners.SelectedValue),
                this.txtAddSerialNO.Text,
                this.txtAddPsamNO.Text,
                this.txtAddRomVersion.Text);
            this.lblError.Text = ret ? "新增成功" : "新增失败";
            this.HideAllPanel();
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 产品报修按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnRequestRepair_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductRepair(
                    int.Parse(dataProducts.SelectedDataKey["Id"].ToString()),
                    int.Parse(comboRequestFault.SelectedValue),
                    int.Parse(comboRealFault.SelectedValue),
                    this.txtRepairMethod.Text,
                    this.txtRepairGear.Text);
            this.lblError.Text = ret ? "维修登记成功" : "维修登记失败";
            this.HideAllPanel();
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 维修完成按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnRepair_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductRepair(
            int.Parse(dataProducts.SelectedDataKey["Id"].ToString()),
            int.Parse(comboRequestFault.SelectedValue),
            int.Parse(comboRealFault.SelectedValue),
            this.txtRepairMethod.Text,
            this.txtRepairGear.Text);
            this.lblError.Text = ret ? "维修成功" : "维修失败";
            this.HideAllPanel();
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 更改设备 新增产品状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ComboAddDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboRealFault.Items.Clear();
            this.comboRequestFault.Items.Clear();
            this.comboRequestFault.Items.Add(new ListItem(string.Empty, "0"));
            this.comboRealFault.Items.Add(new ListItem(string.Empty, "0"));
            foreach (Fault f in Utility.GetFaultsByDeviceId(int.Parse(this.comboAddDevice.SelectedValue)))
            {
                this.comboRequestFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
                this.comboRealFault.Items.Add(new ListItem(f.FaultCode + " " + f.FaultName, f.Id.ToString()));
            }
        }

        /// <summary>
        /// 事件 新增产品并维修登记按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddAndRepair_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductAddAndRepair(
                int.Parse(this.comboAddDevice.SelectedValue),
                int.Parse(this.comboAddOwners.SelectedValue),
                this.txtAddSerialNO.Text,
                this.txtAddPsamNO.Text,
                this.txtAddRomVersion.Text,
                int.Parse(comboRequestFault.SelectedValue),
                0,
                this.txtRepairMethod.Text,
                this.txtRepairGear.Text);

            this.lblError.Text = ret ? "新增并维修登记成功" : "新增并维修登记失败";
            this.HideAllPanel();
            this.dataProducts.DataBind();
        }

        /// <summary>
        /// 事件 新增产品并维修完成按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddAndRepair2_Click(object sender, EventArgs e)
        {
            bool ret = Utility.ProductAddAndRepair(
                int.Parse(comboAddDevice.SelectedValue),
                int.Parse(comboAddOwners.SelectedValue),
                txtAddSerialNO.Text,
                txtAddPsamNO.Text,
                txtAddRomVersion.Text,
                int.Parse(comboRequestFault.SelectedValue),
                int.Parse(comboRealFault.SelectedValue),
                txtRepairMethod.Text,
                txtRepairGear.Text);
            lblError.Text = ret ? "新增并维修完成" : "新增并维修失败";
            this.HideAllPanel();
            dataProducts.DataBind();
        }
    }
}